Thursday, October 30, 2014

R Set Theory Functions

There are several R set theory functions that can be very useful in solving complex problems. You can also use them to verify your solutions to simpler problems.

Most of what you need is in the arules library. You can easily create a complement function of your own.

Set Theory Functions

Let's set up some functions.

library(arules)
## Loading required package: Matrix
## 
## Attaching package: 'arules'
## 
## The following objects are masked from 'package:base':
## 
##     %in%, write
  omega <- 1:6
    A <- c(1,2,3)
    B <- c(3,4,5,6)
    complement <- function(a){setdiff(omega,a)}

Set Calculations

union(A,B)
## [1] 1 2 3 4 5 6
intersect(A,B)
## [1] 3
setdiff(A,B)
## [1] 1 2
intersect(A,complement(B))
## [1] 1 2